package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.DmcCsunSleepCollection;
import com.csun.cmny.provider.model.domain.DmcCsunVersionFile;
import com.csun.cmny.provider.model.dto.csunsleep.CollectionQueryDto;
import com.csun.cmny.provider.model.vo.csunsleep.CollectionVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunSleepCollectionService;
import com.csun.cmny.provider.service.DmcCsunSleepStateService;
import com.csun.cmny.util.DateUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-06-08 14:39
 * @Description:
 */
@RestController
@RequestMapping(value = "/csunSleep", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunSleepCollectionController extends BaseController {

    @Resource
    private DmcCsunSleepStateService dmcCsunSleepStateService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunSleepCollectionService dmcCsunSleepCollectionService;

    /**
     * 床垫采集数据 - 开启
     *
     * @param deviceId  the device id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/openDataCollection/{deviceId}", method = RequestMethod.POST)
    public Wrapper open(@PathVariable Long deviceId) {


        Integer result = dmcCsunSleepStateService.sendDataMsg(deviceId, DeviceConstant.COLLECTION_OPEN);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 床垫采集数据 - 关闭
     *
     * @param deviceId  the device id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/closeDataCollection/{deviceId}", method = RequestMethod.POST)
    public Wrapper close(@PathVariable Long deviceId) {


        Integer result = dmcCsunSleepStateService.sendDataMsg(deviceId, DeviceConstant.COLLECTION_CLOSE);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 查询床垫采集数据列表 - 分页
     *
     * @param collectionQueryDto  the collection query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryCollectionListWithPage", method = RequestMethod.POST)
    public Wrapper queryCollectionListWithPage(@RequestBody CollectionQueryDto collectionQueryDto) {

        List<CollectionVo> collectionVoList = dmcCsunSleepCollectionService.queryCollectionListWithPage(collectionQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(collectionVoList));
    }

    /**
     * 下载采集数据文件
     *
     * @param request
     * @param response
     * @param id
     * @throws IOException
     */
    @RequestMapping(value = "/downCollectionFile/{id}")
    public void downCsunVersionFile(HttpServletRequest request, HttpServletResponse response,
                                    @PathVariable Long id) throws IOException {

        DmcCsunSleepCollection record = dmcCsunSleepCollectionService.selectByKey(id);

        String filename = DateUtil.nowDateStr() + ".txt";

        //设置响应流文件进行下载
        response.setHeader("Content-Disposition","attachment;filename=" + filename);
        response.setHeader("Content-Type", "text/html;charset=utf-8");
        response.setHeader("Content-Length", record.getFileData().length + "");
        response.setHeader("Content-Language", "en");
        ServletOutputStream sos = response.getOutputStream();

        sos.write(record.getFileData());
        sos.flush();
        sos.close();
    }

}
